package com.songqintuan.platform.user;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.songqintuan.common.bean.GenerateVerifyCodeBean;
import com.songqintuan.common.util.DESPlusUtil;
import com.songqintuan.common.util.DateTimeUtil;
import com.songqintuan.common.util.MessageFactory;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.SystemUser;
import com.songqintuan.model.vo.SystemUserVo;
import com.songqintuan.persist.service.hibernate.ISystemUserService;
import com.songqintuan.platform.common.constant.PlatformConstants;
import com.songqintuan.platform.common.constant.PlatformManageBeanConstants;
import com.songqintuan.platform.common.constant.PlatformSpringBeanConstants;

@ManagedBean(name = "userLoginPage")
@ViewScoped
public class UserLoginPageBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SystemUserVo systemUser;
	private List<String> errorMessages = new ArrayList<String>();
	private String verifyCodeForQuestion;
	private String verifyCode;

	public List<String> getErrorMessages() {
		return errorMessages;
	}

	public void setErrorMessages(List<String> errorMessages) {
		this.errorMessages = errorMessages;
	}

	public String getVerifyCodeForQuestion() {
		return verifyCodeForQuestion;
	}

	public void setVerifyCodeForQuestion(String verifyCodeForQuestion) {
		this.verifyCodeForQuestion = verifyCodeForQuestion;
	}

	public String getVerifyCode() {
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	public UserLoginPageBean() {
		if (null == this.getSystemUser()) {
			systemUser = new SystemUserVo();
		}
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil
				.getManagedBean(PlatformManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.getVerifyCode());
	}

	public SystemUserVo getSystemUser() {
		return systemUser;
	}

	public void setSystemUser(SystemUserVo systemUser) {
		this.systemUser = systemUser;
	}

	public String generateVerifyCode() {
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil
				.getManagedBean(PlatformManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.getVerifyCode());
		return null;
	}
	
	public String loginUser() {
		this.getErrorMessages().clear();

		ISystemUserService<SystemUser,String> systemUserService = (ISystemUserService<SystemUser,String>) SpringFacesUtil.getSpringBean(PlatformSpringBeanConstants.SYSTEM_USER_SERVICE_BEAN_NAME);
		systemUserService.setEntityClass(SystemUser.class);
		systemUserService.setPKClass(String.class);
		SystemUser systemUser = systemUserService.findUniqueBy("loginName", this.getSystemUser().getLoginName());
		Timestamp loginTime = DateTimeUtil.getCurrentTimestamp();
		if (null == systemUser) {
			this.getErrorMessages().add(MessageFactory.getMessage(PlatformConstants.USER_BUNDLE_NAME,"user.notexist.error.message", FacesMessage.SEVERITY_ERROR, this.getSystemUser().getLoginName()).getDetail());
			return null;
		}else {
			if(null == systemUser.getStatus() || "".equals(systemUser.getStatus()) || "0".equals(systemUser.getStatus())) {
				this.getErrorMessages().add(MessageFactory.getMessage(PlatformConstants.USER_BUNDLE_NAME,"user.inactive.error.message", FacesMessage.SEVERITY_ERROR, this.getSystemUser().getLoginName()).getDetail());
				return null;
			} 
			String password = (String) this.getSystemUser().getLoginPassword();
			password = DESPlusUtil.encrypt(password);
			String realyPassword = (String) systemUser.getLoginPassword();
			if (!(password.equals(realyPassword))) {
				this.getErrorMessages().add(MessageFactory.getMessage(PlatformConstants.USER_BUNDLE_NAME,"user.password.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
				return null;
			}
			//修改最后登陆时间和上一次登陆时间开始
			//customer.setPreLoginTime(customer.getLastLoginTime());
			//customer.setLastLoginTime(loginTime);
			//customerService.saveOrUpdate(customer);
			//修改最后登陆时间和上一次登陆时间结束
			//customer info set into session
			CurrentUserBean currentUser = (CurrentUserBean)SpringFacesUtil.getManagedBean(PlatformManageBeanConstants.CURRENT_USER_BEAN);
			currentUser.setSystemUser(systemUser);
			//写入LOGINLOG信息  开始
			//LoginLogBean loginLogBean = (LoginLogBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_LOG_BEAN);
			
			//ILoginLogService<LoginLog,Long> loginLogService = (ILoginLogService<LoginLog,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.LOGIN_LOG_SERVICE_BEAN_NAME);
			//loginLogService.setEntityClass(LoginLog.class);
			//loginLogService.setPKClass(Long.class);
			////写入LOGINLOG信息 结束
		}
		return "main";
	}

}
